package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.HashMapUtils;
import com.j283_5.wisdom_park.mapper.ContractTableMapper;
import com.j283_5.wisdom_park.pojo.ContractTable;
import com.j283_5.wisdom_park.pojo.Visitors;
import com.j283_5.wisdom_park.service.ContractTableService;
import com.j283_5.wisdom_park.vo.contract_management.CollectionRecordVo;
import com.j283_5.wisdom_park.vo.contract_management.ContractAddVo;
import com.j283_5.wisdom_park.vo.contract_management.ContractTableVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 合同表 服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class ContractTableServiceImpl implements ContractTableService {

    @Resource
    private ContractTableMapper mapper;

    /**
     * 多条件查询合同信息
     * @param contractTableVo
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Map<String,Object> findContractTable(ContractTableVo contractTableVo, Integer page, Integer limit) {
        Integer start=null;
        //总条数
        Long countRows=null;
        List<ContractTableVo>  list=null;
        if (page!=null&&limit!=null){
            start=(page-1)*limit;
        }
        countRows=mapper.findContractTableCount(contractTableVo);
        //如果总条数大于0  才去查询数据
        if(countRows!=null  && countRows>0){
            list=mapper.findContractTable(contractTableVo,start,limit);
            return new HashMapUtils()
                    .addData("countRows",countRows)
                    .addData("listContractTable",list)
                    .getMap();
        }else{
            return new HashMapUtils().getMap();
        }
    }

    @Override
    public ResultData findContractTableInfoById(Integer contract_id) {
        List<ContractTableVo> contractTableById = mapper.findContractTableById(contract_id);
        List<ContractTableVo> roomById = mapper.findRoomById(contract_id);
        List<CollectionRecordVo> collectionById = mapper.findCollectionById(contract_id);
        Map<String,Object> map=new HashMap<>();
        map.put("contractTableById",contractTableById);
        map.put("roomById",roomById);
        map.put("collectionById",collectionById);
        return ResultData.ok().data(map);
    }

    /**
     * 根据合同id查询房间信息
     * @param contract_id
     * @return
     */
    @Override
    public Map<String, Object> findRoomById(Integer contract_id) {
        List<ContractTableVo> list = mapper.findRoomById(contract_id);
        return new HashMapUtils().addData("RoomVoList",list).getMap();
    }

    /**
     * 根据合同id查询收款信息
     * @param contract_id
     * @return
     */
    @Override
    public Map<String, Object> findCollectionById(Integer contract_id) {
        List<CollectionRecordVo> list = mapper.findCollectionById(contract_id);
        return new HashMapUtils().addData("CollectionList",list).getMap();
    }

    /**
     * 根据合同id删除合同
     * @param contract_id
     * @return
     */
    @Override
    public ResultData deleteContractTable(List<Integer>  contract_id) {

        UpdateWrapper<ContractTable> ContractTableWrapper = new UpdateWrapper<>();
        ContractTableWrapper.set("contract_delete",1);
        ContractTableWrapper.in("contract_id",contract_id);
        int i = mapper.update(null, ContractTableWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.error().message("删除失败！");

    }

    @Override
    public int insertContract(ContractTableVo contractTableVo,ContractAddVo contractAddVo) {
        return mapper.insertContract(contractTableVo,contractAddVo);
    }

    /**
     * 查找要插入的字段id
     * @param contractTableVo
     * @return
     */
    @Override
    public Integer findContractAddPer_IdVo(ContractTableVo contractTableVo) {
        return mapper.findContractAddPer_IdVo(contractTableVo);
    }

    /**
     * 插入租凭房间信息
     * @param contractTableVo
     * @return
     */
    @Override
    public int insertRoom(ContractTableVo contractTableVo, ContractAddVo contractAddVo) {
        return mapper.insertRoom(contractTableVo,contractAddVo);
    }


    /**
     * 插入收帐信息
     * @return
     */
    @Override
    public int insertCollection_record(CollectionRecordVo collectionRecordVo, ContractAddVo contractAddVo) {
        return mapper.insertCollection_record(collectionRecordVo,contractAddVo);
    }

    /**
     * 签订合同
     * @param ContractId
     * @return
     */
    @Override
    public ResultData DoContractTable(List<Integer> ContractId) {
        UpdateWrapper<ContractTable> contractTableUpdateWrapper = new UpdateWrapper<>();
        contractTableUpdateWrapper.set("contract_state",1);
        return null;
    }
}
